package com.znxs.api_management_c.controller;

import com.znxs.api_management_c.common.BaseResponse;
import com.znxs.api_management_c.common.ErrorCode;
import com.znxs.api_management_c.common.ResultUtils;
import com.znxs.api_management_c.exception.BusinessException;
import com.znxs.api_management_c.model.DTO.ApiInfoUploadRequest;
import com.znxs.api_management_c.model.VO.ApiInfoVO;
import com.znxs.api_management_c.service.ApiInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/apiInfo")
public class ApiInfoController {
    @Resource
    private ApiInfoService apiInfoService;

    /**
     * 查询所有api接口
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<ApiInfoVO>> list(){
        return ResultUtils.success(apiInfoService.listApiInfo());
    }

    /**
     * 搜索api接口
     * @param searchTag 搜索关键字
     * @return
     */
    @GetMapping("/search/{searchTag}")
    public BaseResponse<List<ApiInfoVO>> searchApiInfo(@PathVariable(name = "searchTag") String searchTag){
        if (StringUtils.isEmpty(searchTag)) throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数为空");
        return ResultUtils.success(apiInfoService.searchApiInfo(searchTag));
    }

    @PostMapping("/upload")
    public BaseResponse<Integer> upload(@RequestBody ApiInfoUploadRequest apiInfoUploadRequest, HttpServletRequest request){
        return ResultUtils.success(apiInfoService.apiUpload(apiInfoUploadRequest, request));
    }


}
